/**
 * 教练管理 账号管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>支付配置信息</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.id" placeholder="输入教练编号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input size="small" v-model="formInline.name" placeholder="输入教练名字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="id" label="教练编号" width="150">
      </el-table-column>
      <el-table-column sortable prop="name" label="名字" width="100">
      </el-table-column>
      <el-table-column sortable prop="gender" label="性别" width="100">
      </el-table-column>
      <el-table-column sortable prop="username" label="账号" width="100">
      </el-table-column>
      <el-table-column sortable prop="password" label="密码" width="100">
      </el-table-column>
      <el-table-column sortable prop="age" label="年龄" width="100">
      </el-table-column>
      <el-table-column sortable prop="teachAge" label="教龄" width="100">
      </el-table-column>
      <el-table-column sortable prop="introduce" label="个性签名" width="200">
      </el-table-column>
      <el-table-column sortable prop="coachAuth" label="认证" width="150">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteCoach(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="名字" prop="name">
          <el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入名字"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio v-model="editForm.gender" label="0">男</el-radio>
          <el-radio v-model="editForm.gender" label="1">女</el-radio>
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input size="small" v-model="editForm.username" auto-complete="off" placeholder="请输入账号" ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input size="small" v-model="editForm.password" auto-complete="off" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input size="small" v-model="editForm.age" auto-complete="off" placeholder="请输入年龄(单位岁)" type="number"></el-input>
        </el-form-item>
        <el-form-item label="教龄" prop="teachAge">
          <el-input size="small" v-model="editForm.teachAge" auto-complete="off" placeholder="请输入教龄(单位年)" type="number"></el-input>
        </el-form-item>
        <el-form-item label="个性签名" prop="introduce">
          <el-input size="small" v-model="editForm.introduce" auto-complete="off" placeholder="请输入个性签名"></el-input>
        </el-form-item>
        <el-form-item label="认证" prop="coachAuth">
          <el-select size="small" v-model="editForm.coachAuth" placeholder="请选择" class="userRole">
            <el-option v-for="type in coachAuth" :label="type.key" :value="type.value" :key="type.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {CoachList,CoachSave,Coachdelete} from '../../api/payMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      coachAuth: [
        { key: '金牌教练', value: '金牌教练' },
        { key: '帅哥教练', value: '帅哥教练' },
        { key: '美女教练', value: '美女教练' },
        { key: '冠军教练', value: '冠军教练' },
        { key: '肌肉教练', value: '肌肉教练' },
        { key: '腹肌教练', value: '腹肌教练' },
        { key: '游泳教练', value: '游泳教练' },
        { key: '王牌教练', value: '王牌教练' }
      ],
      editForm: {
        id: '',
        password: '',
        username: '',
        gender: '',
        name: '',
        introduce: '',
        age:'',
        teachAge:'',
        coachAuth:'',
        token: localStorage.getItem('logintoken')
      },
      // rules表单验证
      rules: {
        gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
        introduce: [{ required: true, message: '请输入个人简介', trigger: 'blur' }],
        name: [{ required: true, message: '请输入名字', trigger: 'blur' }],
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        coachAuth: [{ required: true, message: '请选择认证', trigger: 'blur' }]
      },
      formInline: {
        page: 1,
        limit: 10,
        id: '',
        name: '',
        token: localStorage.getItem('logintoken')
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取列表
    getdata(parameter) {
      this.loading = true
      CoachList(parameter)
        .then(res => {
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.listData = res.data
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.count
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.password = row.password
        this.editForm.id = row.id
        this.editForm.introduce = row.introduce
        this.editForm.gender = row.gender
        this.editForm.name = row.name
        this.editForm.username = row.username
        this.editForm.age = row.age
        this.editForm.teachAge = row.teachAge
        this.editForm.coachAuth = row.coachAuth
      } else {
        this.title = '添加'
        this.editForm.password = ''
        this.editForm.id = ''
        this.editForm.gender = ''
        this.editForm.introduce = ''
        this.editForm.name = ''
        this.editForm.username = ''
        this.editForm.age = ''
        this.editForm.teachAge = ''
        this.editForm.coachAuth = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          CoachSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '教练信息添加成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('支付配置信息保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除公司
    deleteCoach(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          Coachdelete({id:row.id})
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '教练已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('教练信息删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 